# CROSS ?= arm-linux-gnueabihf

GCC ?= $(CROSS)gcc
GXX ?= $(CROSS)g++
ifneq ($(CROSS),)
	GCC = $(CROSS)-gcc
	GXX = $(CROSS)-g++
endif

# 模式选择 
# IPCSELECT ?= zmq
IPCSELECT ?= nanomsg

ifeq ("$(IPCSELECT)","zmq")
CFLAGS += -lzmq -lrt
CFLAGS += -DIPCAPI_USE_ZMQ
endif
ifeq ("$(IPCSELECT)","nanomsg")
CFLAGS += -lnanomsg -lanl -lrt -lnsl
CFLAGS += -DIPCAPI_USE_NANOMSG
endif

ROOT = $(shell pwd)

test:
	@$(GXX) -o $(ROOT)/out $(ROOT)/src/main.cpp $(ROOT)/src/ipcapi.c \
	-I$(ROOT)/src -I$(ROOT)/install/include -L$(ROOT)/install/libs/x64_linux \
	$(CFLAGS) -lpthread -Wall

test2:
	@$(GXX) -o $(ROOT)/out $(ROOT)/src/main2.cpp $(ROOT)/src/ipcapi.c \
	-I$(ROOT)/src -I$(ROOT)/install/include -L$(ROOT)/install/libs/x64_linux \
	$(CFLAGS) -lpthread -Wall

clean:
	@rm -rf out*

cleanall: clean
	@rm -rf ./libs/*

libs: zmq nanomsg

# sudo apt-get install libtool gawk
zmq:
	@rm -rf $(ROOT)/libs/libzmq-4.3.4 && \
	tar -xzf $(ROOT)/pkg/libzmq-4.3.4.tar.gz -C $(ROOT)/libs && \
	cd $(ROOT)/libs/libzmq-4.3.4 && \
	./autogen.sh && \
	./configure \
		--prefix=$(ROOT)/libs \
		--host=$(CROSS) \
		--with-pic && \
	make -j4 && make install &&\
	cd -	

# 启用动态库时删除配置 NN_STATIC_LIB
nanomsg:
	@rm -rf $(ROOT)/libs/nanomsg-1.1.5 && \
	tar -xzf $(ROOT)/pkg/nanomsg-1.1.5.tar.gz -C $(ROOT)/libs && \
	cd $(ROOT)/libs/nanomsg-1.1.5 && \
	cmake \
		-DCMAKE_INSTALL_PREFIX=$(ROOT)/libs \
		-DCMAKE_C_COMPILER=$(GCC) \
		-DCMAKE_CXX_COMPILER=$(GXX) \
		-DCMAKE_C_FLAGS="-fPIC" \
		-DCMAKE_CXX_FLAGS="-fPIC" \
		-DNN_STATIC_LIB=1 \
		-DNN_TOOLS=0 \
		-DNN_ENABLE_NANOCAT=0 && \
	make -j4 && make install &&\
	cd -
